﻿Imports System.Drawing
Imports System.Runtime.InteropServices

Public Class FolderDeleteOptions

    Public Event Close(ByVal sender As FolderDeleteOptions)
    Public Event Change(ByVal sender As FolderDeleteOptions)

    Public Sub New()
        InitializeComponent()
        pbClose.Image = Me.pbClose.ErrorImage
        Me.TimeUnits = 0
        Me.TimeValue = 1
    End Sub

    Public Sub New(ByVal Close As Bitmap)
        InitializeComponent()
        pbClose.Image = Close
        Me.TimeUnits = 0
        Me.TimeValue = 1
    End Sub

    Public Sub New(ByVal Close As Bitmap, ByVal Values As RegistryStoredFolderValues)
        InitializeComponent()
        pbClose.Image = Close
        Me.FolderPath = Values.FolderPath
        Me.TimeValue = Values.TimeValue
        Me.TimeUnits = Values.TimeUnits
        Me.Disabled = Values.Disabled
    End Sub

    Public Sub New(ByVal Close As Bitmap, ByVal FolderPath As String, ByVal TimeValue As Integer, _
                   ByVal TimeUnits As Byte, ByVal Disabled As Boolean)
        InitializeComponent()
        pbClose.Image = Close
        Me.FolderPath = FolderPath
        Me.TimeValue = TimeValue
        Me.TimeUnits = TimeUnits
        Me.Disabled = Disabled
    End Sub

    Public Property FolderPath As String
        Get
            Return Me.txtFolderName.Text
        End Get
        Set(ByVal value As String)
            Dim f As Outlook.Folder = ThisAddIn.GetFolderByPath(value)
            If f IsNot Nothing Then
                txtFolderName.Text = value
                txtFolderName.Tag = f
            End If
        End Set
    End Property

    Public ReadOnly Property KeyName As String
        Get
            Return FolderPath.Replace("\"c, "$"c)
        End Get
    End Property

    Public Property TimeValue As UInteger
        Get
            Return nudTimeValue.Value
        End Get
        Set(ByVal value As UInteger)
            If value >= nudTimeValue.Minimum AndAlso value <= nudTimeValue.Maximum Then
                nudTimeValue.Value = value
            End If
        End Set
    End Property

    Public Property TimeUnits As Byte
        Get
            Return ddTimeUnits.SelectedIndex
        End Get
        Set(ByVal value As Byte)
            If value < ddTimeUnits.Items.Count Then
                ddTimeUnits.SelectedIndex = value
            End If
        End Set
    End Property

    Public Property Disabled As Boolean
        Get
            Return Me.cbDisable.Checked
        End Get
        Set(ByVal value As Boolean)
            Me.cbDisable.Checked = value
        End Set
    End Property

    Private Sub btnFolder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFolder.Click
        Dim f As Outlook.Folder
        f = CType(CreateObject("Outlook.Application"), Outlook.Application).GetNamespace("MAPI").PickFolder()
        If f Is Nothing Then Exit Sub
        Me.txtFolderName.Text = f.FolderPath
        Me.txtFolderName.Tag = f
    End Sub

    Private Sub pbClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pbClose.Click
        RaiseEvent Close(Me)
    End Sub

    Private Sub OnChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles cbDisable.CheckedChanged, ddTimeUnits.SelectedValueChanged, nudTimeValue.ValueChanged, _
                txtFolderName.TextChanged
        RaiseEvent Change(Me)
    End Sub

    Private Sub FolderDeleteOptions_Close(ByVal sender As FolderDeleteOptions) Handles Me.Close
        If Me.txtFolderName.Tag IsNot Nothing Then Marshal.ReleaseComObject(Me.txtFolderName.Tag)
    End Sub
End Class
